package nuder;

import titt.TITTNuder;
import titt.server.RunServer;
import titt.service.content.NuderRequest;
import titt.service.content.NuderResponse;
import titt.util.Bytes;
import js.Dev;
import js.Nuder;

public class ProxyTest {
	
	public static void main(String[] args) throws Exception {
		
		RunServer.run(8080, "localhost");
		
		TITTNuder.register(new Dev(), "*");
		TITTNuder.register(new Nuder(), "nude", "test");

		Dev proxy = TITTNuder.createProxy("*");
		
		NuderResponse resp = proxy.get("/index.html");
		
		System.out.println("the content type is   : " + resp.getHead().getContentType());
		System.out.println("the content length is : " + resp.getLength());
		
		byte[] data = new byte[resp.getLength()];
		resp.getStream().read(data);
		
		System.out.println("the content is        :");
		System.out.println(Bytes.asString(data).substring(0, Math.min(10, Bytes.asString(data).length())) + "...");
		
		Nuder anotherProxy = TITTNuder.createProxy("nude", "*");
		
		System.out.println("posting to another proxy");
		
		resp = anotherProxy.post(10, new NuderRequest(data));

		data = new byte[resp.getLength()];
		resp.getStream().read(data);
		
		System.out.println("the content is        :");
		System.out.println(Bytes.asString(data).substring(0, Math.min(10, Bytes.asString(data).length())) + "...");

		
		System.exit(0);
	}

}
